<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/2/7
 * Time: 上午10:43
 */

namespace app\api\controller;


use app\common\controller\Platurl;
use app\common\model\Agent;
use app\common\model\CardMember;
use app\common\model\Merchant;
use app\common\model\Momerchant;
use app\common\model\System;
use app\common\model\WxOpen;
use app\common\model\Yuyue;
use think\Controller;
use Wechat\Wxobjopen;

class Xcxinfo extends Controller
{
    public function machinfo($arr)
    {

        $Momerchant = new Momerchant();
        $map['id'] = $arr['merchid'];
        $info = $Momerchant->where($map)->find();
        $Platurl = new Platurl();
        $agenturl = $Platurl->getagenturl('top_mid',$arr['merchid']);
//        $info['img'] = $agenturl.$info['img'];
        $info['pictures']=explode(',',$info['pictures']);
        return json($info);

    }

    public function yuyue($arr)
    {
//        file_put_contents('/data2/html/payos/fireuser.txt', 'yuyue-1' . json_encode($arr), FILE_APPEND);
        $Yuyue = new Yuyue();
        $map['openid'] = $arr['openid'];
        $infos = $Yuyue->where($map)->order('id desc')->find();

        if(time()-$infos['addtime']>30*60){
            $mab['id']=$arr['mid'];
            $merchname=Merchant::where($mab)->value('merchant');

            $data['xingming'] = $arr['xingming'];
            $data['sex'] = $arr['sex'];
            $data['yytime'] = $arr['yytime'];
            $data['addtime'] = time();
            $data['tel'] = $arr['tel'];
            $data['mid'] = $arr['mid'];
            $data['merchid'] = $arr['merchid'];
            $data['merchname'] = $merchname;
            $data['openid'] = $arr['openid'];
            $data['remark'] = $arr['remark'];

            $add = $Yuyue->save($data);
            if ($add) {
                $options = $this->options2($arr['merchid']);

                $wxObj = new Wxobjopen($options);

                $data['keyword1'] = array(
                    "value" => $arr['xingming'],
                    "color" => "#DEB887"
                );
                $data['keyword2'] = array(
                    "value" => $arr['tel'],
                    "color" => "#DEB887"
                );
                $data['keyword3'] = array(
                    "value" => $merchname, //门店
                    "color" => "#DEB887"
                );
                $data['keyword4'] = array(
                    "value" => $arr['yytime'],//时间
                    "color" => "#DEB887"
                );
                $data['keyword5'] = array(
                    "value" => $arr['remark'],
                    "color" => "#DEB887"
                );
                $xcxopenid=Momerchant::where(['id'=>$arr['merchid']])->value('wx_open_id_xcx');
                $openinfo=WxOpen::where(['id'=>$xcxopenid])->find();
                $param['touser'] =  $openinfo['xcx_yy_admin'];
                $param['template_id'] = $openinfo['xcx_yy_tpl'];
                $param['form_id'] = $arr['formId'];
                $param['data'] = $data;
                $s=$wxObj->xcx_sendmessage($param);
                if($s){
                    $datas['yy_tpl_info']=json_encode($s);
                    $Yuyue->save($datas,['id'=>$Yuyue->id]);
                }
                $msg['code'] = 0;
                $msg['message'] = "预约成功";
            } else {
                $msg['code'] = 1;
                $msg['message'] = "预约失败";
            }}else{
            $msg['code'] = 2;
            $msg['message'] = "预约时间间隔过短";
        }
        return json($msg);


    }

    public function xcx_login($arr)
    {

        $options = $this->options2($arr['merchid']);

        $wxObj = new Wxobjopen($options);
        $param['code'] = $arr['code'];
//return json($arr);
        $errcode = $wxObj->xcx_login($param);

        return json($errcode);


    }

    //获取card_id
    public function get_card_id(){
        $mid = request()->param('mid');
        $CardMember = new CardMember();
        $card['card_id'] = $CardMember->where(['top_mid'=>$mid])->value('card_id');
        if ($card){
            $card['code'] = 0;
            $card['message'] = "success";
        }else{
            $card['code'] = 9001;
            $card['message'] = "error";
        }
        return json($card);
    }

    public function get_merlist($arr){
        $Merchant = new Merchant();

        $list['data'] = $Merchant->where(['top_mid'=>$arr['merchid']])->field('id,merchant')->select();
        if ($list){
            $list['code'] = 0;
            $list['message'] = "success";
        }else{
            $list['code'] = 9001;
            $list['message'] = "error";
        }
        return json($list);

    }

    public function options2($top_mid)
    {
        $Momerchant = new Momerchant();
        $System = new System();
        $WxOpen = new WxOpen();
        $Agent=new Agent();

        $wx_open_id = $Momerchant->where('id', $top_mid)->value('wx_open_id_xcx');
        $wx_open_id2 = $Momerchant->where('id', $top_mid)->value('wx_open_id');
        $agent_id = $Momerchant->where('id', $top_mid)->value('agent');
//        $config = $System->where(['id' => 1])->find();
        $users = $WxOpen->where(['id' => $wx_open_id])->find();
//        $users2 = $WxOpen->where(['id' => $wx_open_id2])->find();
        $config=$Agent->where(array('id'=>$agent_id))->find();
        $options = array(
            'component_token' => $config['component_token'], // 填写你设定的key
            'component_encodingaeskey' => $config['component_encodingaeskey'], // 填写加密用的EncodingAESKey
            'component_appid' => $config['component_appid'], // 填写高级调用功能的app id
            'component_appsecret' => $config['component_appsecret'], // 填写高级调用功能的密钥
            'open_ticket' => $config['open_ticket'],
            'authorizer_appid' => $users['authorizer_appid'],
            'authorizer_refresh_token' => $users['authorizer_refresh_token'],
        );
        return $options;
    }
}